package lib.bone.nepudata.service.impl;

import cn.hutool.core.lang.Validator;
import lib.bone.access.constant.ResearchTypeConst;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.exception.BusinessException;
import lib.bone.nepudata.dao.NepuPropertyDAO;
import lib.bone.nepudata.domain.entity.NepuProperty;
import lib.bone.nepudata.expose.WorkloadService;
import lib.bone.nepudata.service.ResearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author: kindear
 */
@Service
public class NepuPropertyServiceImpl implements ResearchService<NepuProperty,String> {

    @Autowired
    NepuPropertyDAO nepuPropertyDAO;

    @Autowired(required = false)
    WorkloadService workloadService;

    @Override
    public List<NepuProperty> getByIdIn(List<String> ids) {
        return nepuPropertyDAO.findByIdIn(ids);
    }

    @Override
    public String getProjectCode(String s) {
        Optional<NepuProperty> entityOp = getById(s);
        if (entityOp.isPresent()){
            return entityOp.get().getPropertyCode();
        }else {
            return "无";
        }
    }

    @Override
    public String getServiceType() {
        return ResearchTypeConst.PROPERTY;
    }

    @Override
    public PageResultDTO<NepuProperty> getAll(Pageable pageable, Example<NepuProperty> example) {
        Page<NepuProperty> page;
        //是否为空 --
        if(Validator.isEmpty(example)){
            page = nepuPropertyDAO.findAll(pageable);
        }else {
            page = nepuPropertyDAO.findAll(example,pageable);
        }
        List<NepuProperty> list = page.getContent();
        PageResultDTO<NepuProperty> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());

        return pageResultDTO;
    }

    public Optional<NepuProperty> findByPropertyCode(String code) {
        return nepuPropertyDAO.findByPropertyCode(code);
    }

    @Override
    public ResponseDTO deleteById(String id) {

        Integer useCount = workloadService.countByLinkId(id);
        // Long useCount = nepuResearchWorkloadDAO.countByResearchTypeAndMapperId("教学科研获奖",id);
        if(useCount > 0){
            return ResponseDTO.wrap(ResponseCodeConst.FORBIDDEN,"存在关联该奖励的科研工作量信息,拒绝执行删除!");
        }
        //判断数据是否存在
        Optional<NepuProperty> nepuPropertyOp = getById(id);
        if(!nepuPropertyOp.isPresent()) return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"ID:["+id+"]对应数据不存在");
        nepuPropertyDAO.deleteById(id);
        return ResponseDTO.succMsg("删除成功!");
    }

    @Override
    public Optional<NepuProperty> getById(String id) {
        return nepuPropertyDAO.findById(id);
    }

    @Override
    public NepuProperty saveOne(NepuProperty nepuProperty) {
        if (Validator.isNotEmpty(nepuProperty.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return nepuPropertyDAO.save(nepuProperty);
    }

    @Override
    public NepuProperty updateOne(NepuProperty nepuProperty) {
        //@TODO 如果在审核流程中 应拒绝修改，或者修改后退回初始流程位置
        if (Validator.isEmpty(nepuProperty.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return nepuPropertyDAO.save(nepuProperty);
    }
}
